local chengxi = fk.CreateSkill{
    name = "jin_heg__chenxi",
}

Fk:loadTranslationTable{
    ["jin_heg__chenxi"] = "乘隙",
    [":jin_heg__chenxi"] = "准备阶段，你可以令一名角色视为使用【以逸待劳】，然后若以此法弃置的牌中包含非基本牌，则此牌使用者对所有目标角色各造成一点伤害。",
    ["#jin_chenxi-choose"] = "乘隙：选择一名角色视为使用【以逸待劳】",

    ["$jin_heg__chenxi1"] = "考其遗法，肃若神明。",
    ["$jin_heg__chenxi2"] = "气韵生动，出于天成。",
}

local H = require "packages.ol_hegemony.util"

chengxi:addEffect(fk.EventPhaseStart,{
    anim_type = "drawcard",
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(chengxi.name) and player.phase == Player.Start
      end,
      on_cost = function(self,event,target,player,data)
        local room = player.room
        local to = room:askToChoosePlayers(player,{
            targets = room.alive_players,
            min_num = 1,
            max_num = 1,
            prompt = "#jin_chenxi-choose",
            skill_name = chengxi.name,
            cancelable = true,
        })
        if #to > 0 then
         event:setCostData(self, {tos = to[1]})
          return true
        end
      end,
      on_use = function(self, event, target, player, data)
        local room = player.room
        local user = event:getCostData(self).tos
        local targets = table.filter(room.alive_players, function(p) return H.compareKingdomWith(p, user) end)
        local targetss = table.filter(room.alive_players, function(p) return H.compareKingdomWith(p, user) end)
        room:notifySkillInvoked(user, chengxi.name, "masochism")
        local preuse = room:useVirtualCard("await_exhausted", {}, user, targets, chengxi.name)
        if preuse and preuse.extra_data and preuse.extra_data.jin_heg__chenxi_record then
          local mark = preuse.extra_data.jin_heg__chenxi_record
            if #mark > 0 and not player.dead and not user.dead then
              room:sortByAction(targetss)
              for _, p in ipairs(targetss) do
              if not p.dead then
              room:damage{
                from = user,
                to = p,
                damage = 1,
                skillName = chengxi.name,
              }
            end
          end
        end
      end
    end,
})

chengxi:addEffect(fk.AfterCardsMove,{
    can_refresh = function(self, event, target, player, data)
        for _, move in ipairs(data) do
          if move.from and move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard then return true end
        end
    end,
    on_refresh = function (self, event, target, player, data)
          local e = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
          if e then
            local use = e.data
            if table.contains(use.card.skillNames, "jin_heg__chenxi") then
              local exdata = e.data.extra_data or {}
              exdata.jin_heg__chenxi_record = exdata.jin_heg__chenxi_record or {}
              for _, move in ipairs(data) do
                if move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard then
                  for _, info in ipairs(move.moveInfo) do
                    local card = Fk:getCardById(info.cardId)
                    if ( card.type ~= Card.TypeBasic )  then
                      table.insertIfNeed(exdata.jin_heg__chenxi_record, info.cardId)
                    end
                  end
                end
              end
           e.data.extra_data = exdata
        end
      end
    end,
})

return chengxi